package com.yc.springmvc.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.*;

public class MyWebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{MvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
//    @Override
//    public void onStartup(ServletContext servletContext) throws ServletException {
//        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
//        ctx.register(MvcConfig.class);
//
//        DispatcherServlet dispatcherServlet = new DispatcherServlet((WebApplicationContext) ctx);
//
////        MyDispatchServlet dispatcherServlet = new MyDispatchServlet();
//
//        ServletRegistration.Dynamic registration = servletContext.addServlet("app",dispatcherServlet);
//        registration.setLoadOnStartup(1);
//        //映射所有的地址
//        registration.addMapping("/");
//
//        //添加文件上传配置
//        MultipartConfigElement mc = new MultipartConfigElement("/");
////        mc.getLocation(); //临时文件存放目录
////        mc.getMaxRequestSize(); //最大请求大小  请求体内总文件大小  上传多个文件
////        mc.getFileSizeThreshold(); //保存临时文件大小的边界值
//        registration.setMultipartConfig(mc);
//
//    }


    //添加文件上传配置
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        MultipartConfigElement mc = new MultipartConfigElement("/");
        /*mc.getLocation(); //临时文件存放目录
        mc.getMaxRequestSize(); //最大请求大小  请求体内总文件大小  上传多个文件
        mc.getFileSizeThreshold(); //保存临时文件大小的边界值*/
        registration.setMultipartConfig(mc);
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }


}
